﻿using UnityEngine;
using System.Collections.Generic;

namespace UI
{
    public class UIActivityPanel : MonoBehaviour
    {
        //Dictionary<ActivityType, UIActivityTemplate> activitys = new Dictionary<ActivityType, UIActivityTemplate>();

        UIActivityTemplate curActivity;

        /// <summary>
        /// 当前激活类型
        /// </summary>
        public ActivityType activedType = ActivityType.None;

        #region 模版页面
        //public GameObject everyDaySignTemplate, firstRechargeTemplate, openServerRaceTemplate, loginAwardTemplate, asunaKitchenTemplate, rechargeSurpriseTemplate, monthCardTemplate, vipBenefitsTemplate, battleRaceTemplate, benefitTemplate, onSailTemplate, openSeverMoney, festivalTemplete, fortuneCatTemplete;
        #endregion
        
        /// <summary>
        /// 模块锚点
        /// </summary>
        public Transform moduleTrans;

        public void Init(List<ActivityType> activityTypes)
        {
            //foreach(var kvp in activitys)
            //{
            //    var obj = kvp.Value.gameObject;
            //    obj.SetActive(false);
            //}

            //foreach(var at in activityTypes)
            //{
            //    InitActivity(at);
            //}
            
        }

        public void Refersh()
        {
            curActivity.Refersh();
            //activitys[activedType].Refersh();
        }
        private void InitActivity(ActivityType at)
        {
            //if (!activitys.ContainsKey(at))
            //{
                GameObject temp = null;
                string path = "";
                switch (at)
                {
                    case ActivityType.EveryDaySign:
                        path = "ActivityInnerPage/every day sign";
                        //temp = everyDaySignTemplate;
                        break;
                    case ActivityType.AsunaKitchen:
                        path = "ActivityInnerPage/asuna kitchen";
                        //temp = asunaKitchenTemplate;
                        break;
                    case ActivityType.FirstRecharge:
                        path = "ActivityInnerPage/first recharge";
                        //temp = firstRechargeTemplate;
                        break;
                    case ActivityType.LoginAward:
                    case ActivityType.OpenHappy:
                    case ActivityType.Congratulation:
                        path = "ActivityInnerPage/login award";
                        //temp = loginAwardTemplate;
                        //temp.GetComponent<UIActivityLoginAward>().type = at;
                        break;
                    case ActivityType.OpenServerRace:
                        path = "ActivityInnerPage/open server race";
                        //temp = openServerRaceTemplate;
                        break;
                    case ActivityType.CostTotal:
                    case ActivityType.RechargeTotal:
                    case ActivityType.RechargeSurprise:
                    case ActivityType.WarriorRescruitTimes:
                    case ActivityType.KaiGongActivity:
                        path = "ActivityInnerPage/recharge surprise";
                        //temp = rechargeSurpriseTemplate;
                        //temp.GetComponent<UIActivityRechargeSurprise>().type = at;
                        break;
                    case ActivityType.VIPBenefits:
                        path = "ActivityInnerPage/vipBenefits";
                        //temp = vipBenefitsTemplate;
                        break;
                    case ActivityType.MonthCard:
                        path = "ActivityInnerPage/MonthCardActivity";
                        //temp = monthCardTemplate;
                        break;
                    case ActivityType.RobTrea:
                    case ActivityType.PrizeTask:
                    case ActivityType.PrizeBoss:
                    case ActivityType.MainCopy:
                    case ActivityType.BattleRace:
                    case ActivityType.AincradStars:
                        path = "ActivityInnerPage/BattleRace";
                        //temp = battleRaceTemplate;
                        //temp.GetComponent<UIActivityBattleRace>().type = at;
                        break;
                    case ActivityType.HalfDiscountSale:
                    case ActivityType.DiscountSale:
                    case ActivityType.TimeLimitExchange:
                    case ActivityType.TimeLimitSail:
                        path = "ActivityInnerPage/OnSail";
                        //temp = onSailTemplate;
                        //temp.GetComponent<UIActivityOnSail>().type = at;
                        break;
                    case ActivityType.CopyBenefits:
                    case ActivityType.WarriorWeightImprove:
                    case ActivityType.RechangeDouble:
                        path = "ActivityInnerPage/CopyBenefit";
                        //temp = benefitTemplate;
                        //temp.GetComponent<CopyBenefit>().activityId = "allCopyDouble";
                        break;
                    case ActivityType.OpenSeverMoney:
                        path = "ActivityInnerPage/OpenSeverMoney";
                        //temp = openSeverMoney;
                        break;
                    case ActivityType.Festival:
                    case ActivityType.Update:
                        path = "ActivityInnerPage/Festival";
                        //temp = festivalTemplete;
                        //temp.GetComponent<UIActivityFestival>().activityType = at;
                        break;
                    case ActivityType.fortuneCat:
                        path = "ActivityInnerPage/treasurecast";
                        //temp = fortuneCatTemplete;
                        break;
                    case ActivityType.continueRechargeGift:
                        path = "ActivityInnerPage/continueChargeGift";
                        break;
                    case ActivityType.CollectionWarrior:
                        path= "ActivityInnerPage/collection warrior";
                        break;
                }

                temp = AssetLoad.ResourceManager.Instance.LoadResource(path) as GameObject;

                switch (at)
                {
                    case ActivityType.LoginAward:
                    case ActivityType.OpenHappy:
                    case ActivityType.Congratulation:
                        temp.GetComponent<UIActivityLoginAward>().type = at;
                        break;
                    case ActivityType.CostTotal:
                    case ActivityType.RechargeTotal:
                    case ActivityType.RechargeSurprise:
                    case ActivityType.WarriorRescruitTimes:
                    case ActivityType.KaiGongActivity:
                        temp.GetComponent<UIActivityRechargeSurprise>().type = at;
                        break;
                    case ActivityType.RobTrea:
                    case ActivityType.PrizeTask:
                    case ActivityType.PrizeBoss:
                    case ActivityType.MainCopy:
                    case ActivityType.BattleRace:
                    case ActivityType.AincradStars:
                        temp.GetComponent<UIActivityBattleRace>().type = at;
                        break;
                    case ActivityType.HalfDiscountSale:
                    case ActivityType.DiscountSale:
                    case ActivityType.TimeLimitExchange:
                    case ActivityType.TimeLimitSail:
                        temp.GetComponent<UIActivityOnSail>().type = at;
                        break;
                    case ActivityType.CopyBenefits:
                    //case ActivityType.WarriorWeightImprove:
                        temp.GetComponent<CopyBenefit>().activityId = "allCopyDouble";
                        temp.GetComponent<CopyBenefit>().activityType = at;
                    break;
                    case ActivityType.WarriorWeightImprove:
                    case ActivityType.RechangeDouble:
                        temp.GetComponent<CopyBenefit>().activityType = at;
                        break;
                    case ActivityType.Festival:
                    case ActivityType.Update:
                        temp.GetComponent<UIActivityFestival>().activityType = at;
                        break;
                    case ActivityType.CollectionWarrior:
                        temp.GetComponent<UIActivityCollectionWarrior>().activityType = at;
                        break;
                }

                GameObject inst = Instantiate(temp);
                inst.transform.SetParent(moduleTrans);
                inst.transform.localRotation = Quaternion.Euler(Vector3.zero);
                inst.transform.localPosition = Vector3.zero;
                inst.transform.localScale = Vector3.one;
                inst.name = at.ToString();

            curActivity = inst.GetComponent<UIActivityTemplate>();
            curActivity.gameObject.SetActive(true);
            curActivity.Init(GetComponent<UIActivity>());
            curActivity.Hide();

            //activitys[at] = inst.GetComponent<UIActivityTemplate>();
            //}

            //activitys[at].gameObject.SetActive(true);
            //activitys[at].Init(GetComponent<UIActivity>());
            //activitys[at].Hide();
        }
        

        public void Show(ActivityType type)
        {
            if (activedType != ActivityType.None)
            {
                Destroy(curActivity.gameObject);
                //activitys[activedType].Hide();
            }

            activedType = type;
            if (activedType != ActivityType.None)
            {
                InitActivity(activedType);
                if (GetComponent<UIActivity>().activityIds.ContainsKey(activedType))
                {
                    curActivity.activityId = GetComponent<UIActivity>().activityIds[activedType];
                    //activitys[activedType].activityId = GetComponent<UIActivity>().activityIds[activedType];
                }
                curActivity.Show();
                //activitys[activedType].Show();
                GetComponent<UIActivity>().PlayTweener();
            }
        }

        public void OnPageClosed()
        {
            curActivity.OnPageClosed();
            //foreach(var actived in activitys)
            //{
            //    actived.Value.OnPageClosed();
            //}
        }

        /// <summary>
        /// 通知页面
        /// </summary>
        public void OnPageCoverPageRemoved()
        {
            curActivity.OnCoverPageRemove();
            //foreach(var actived in activitys)
            //{
            //    actived.Value.OnCoverPageRemove();
            //}
        }

        public void OnPageCoverPageOpen()
        {
            curActivity.OnCoverPageOpen();
        }

    }
}
